#include "HUD.h"

#ifdef DX9

#include "NeroGame\NGame.h"
#include "NeroEngine\NUtility.h"
#include "NeroEngine\NPlatform.h"

#if WIN_32
#include "Win32_DX9\Win32ColorHelper.h"
#include "Win32_DX9\Win32Platform.h"
#include "Win32_DX9\Win32Device.h"
#endif

using namespace Graphics;

RTTI_DEFINITIONS(HUD)

HUD::HUD()
	:Win32RenderEntity()
{
	mDiplayText.reserve(1);
}

HUD::~HUD(void)
{
#ifdef DX9
	if(mFont != NULL)
	{
		mFont->Release();
	}
#elif defined(DX11)

#endif
	mEnabled = false;
}

void HUD::Update(float gameTime)
{	
	mDiplayText.clear();
	AddText<double>("FramesPerSecond:    ",  GetPlatform()->GetFPS(), 0.0f, 0.0f);
	AddText<double>("TotalGameTime:      ", GetPlatform()->GetGameTime(), 0.0f, 20.0f);
	AddText<double>("ElapsedGameTime:    ",  GetPlatform()->GetDeltaTime() *1000, 0.0f, 40.0f);
}

void HUD::Initialize()
{
	System::Win32Platform* platform = (System::Win32Platform*)GPlatform;
	if(platform)
	{
		System::Win32Device* device = platform->GetDevice();
#ifdef DX9
		D3DXCreateFont(device->GetDevice(),			// the D3D Device
					   20,							// font height
					   0,							// deault font width
					   FW_NORMAL,					// font weight
					   1,							// not using MipLevels
					   false,						// italic FLOATont
					   DEFAULT_CHARSET,				// default character set
					   OUT_DEFAULT_PRECIS,			// default OutputPrecision,
					   DEFAULT_QUALITY,				// default Quality
					   DEFAULT_PITCH | FF_DONTCARE, // default pitch and family
					   "Arial",						// use facename Arial
					   &mFont );					// the font object
#elif defined(DX11)

#endif

	}
	mEnabled = true;
}

void HUD::Render(float gameTime)
{
	if(mDiplayText.size() != 0)
	{
		std::vector<HudEntry>::iterator it = mDiplayText.begin();
		for(int i = 0; it != mDiplayText.end(); it++,i++)
		{
			PrintText((*it).message.c_str(),30,(int)(*it).x,(int)(*it).y,Win32ColorHelper::White);
		}
	}
}

void HUD::PrintText(const char* text, int size, int x, int y, DWORD color)
{
	if(text != NULL)
	{
		if(GPlatform)
		{
			System::Win32Platform* winPlatform = (System::Win32Platform*)GPlatform;
			if(winPlatform)
			{
				System::Win32Device* device = winPlatform->GetDevice();
				RECT textbox;
				SetRect(&textbox, x, y, (int) device->GetResolution().x, (int) device->GetResolution().y);
				mFont->DrawTextA(NULL,text,strlen(text),&textbox, DT_LEFT | DT_TOP, color);
			}
		}
	}
}

void HUD::AddText(HudEntry text)
{
	if(mEnabled == true && text.message != "")
	{
		mDiplayText.push_back(text);
	}
}

void HUD::AddText(const char* label, float x, float y)
{
	mStringStream.str("");
	mStringStream.clear();
	mStringStream << label;
	
	HudEntry entry;
	entry.message = mStringStream.str();
	entry.x = x;
	entry.y = y;

	AddText(entry);
}

#endif